package com.oyjd.ybx.business.classify.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oyjd.ybx.bean.po.Classify;
import com.oyjd.ybx.bean.vo.ClassifyVO;
import com.oyjd.ybx.business.classify.dao.ClassifyDao;
import com.oyjd.ybx.business.classify.service.ClassifyService;
import com.oyjd.ybx.fw.action.Page;

/**
 * 
 * 商品分类业务接口实现
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
@Transactional(readOnly = true)
public class ClassifyServiceImpl implements ClassifyService {
	private ClassifyDao dao;

	@Autowired
	public void setDao(ClassifyDao dao) {
		this.dao = dao;
	}

	/**
	 * 获取商品分类列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	public Page<ClassifyVO> getList(Page<ClassifyVO> page) {
		return dao.getList(page);
	}

	/**
	 * 根据ID获取商品分类对象信息
	 * 
	 * @param id
	 *            ID
	 * @return
	 */
	public ClassifyVO getClassifyVO(String classId) {
		return dao.getClassifyVO(classId);
	}

	/**
	 * 增加商品分类
	 * 
	 * @param className
	 *            分类名称
	 * @param pid
	 *            父分类
	 */
	@Transactional(readOnly = false)
	public void add(String className, String pid) {
		Classify classify = new Classify();
		classify.setClassName(className);
		classify.setPid(pid);
		classify.setOrderKey(System.currentTimeMillis());
		dao.create(classify);
		getFrontClassByDb();
	}

	/**
	 * 修改商品分类
	 * 
	 * @param classId
	 *            分类ID
	 * @param className
	 *            分类名称
	 */
	@Transactional(readOnly = false)
	public void edit(String classId, String className) {
		Classify classify = (Classify) dao.queryById(Classify.class, classId);
		classify.setClassName(className);
		dao.update(classify);
		getFrontClassByDb();
	}

	/**
	 * 删除商品分类
	 * 
	 * @param id
	 *            ID
	 * @return
	 */
	@Transactional(readOnly = false)
	public boolean remove(String id) {
		boolean flag = dao.remove(id);
		getFrontClassByDb();
		return flag;
	}

	/**
	 * 获取树结构数据
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public JSONArray getTypeTree() {
		List<Classify> list = (List<Classify>) dao.queryByHql("from " + Classify.class.getName());
		return getLevelByPid(list, "");
	}

	/**
	 * 获取一个节点下所有子节点形成的JSONArray
	 * 
	 * @param sou
	 * @param parentId
	 * @return
	 */
	private static JSONArray getLevelByPid(List<Classify> sou, String id) {
		JSONArray result = new JSONArray();
		for (Classify q : sou) {
			if (id.equals(q.getPid())) {
				JSONObject jobj = new JSONObject();
				jobj.put("id", q.getClassId());
				jobj.put("text", q.getClassName());
				JSONObject att = new JSONObject();
				att.put("pid", q.getPid());
				jobj.put("attributes", att);
				JSONArray tmp = getLevelByPid(sou, q.getClassId());
				if (tmp.size() > 0) {
					jobj.put("state", "closed");
					jobj.put("children", tmp);
				}
				result.add(jobj);
			}
		}
		return result;
	}

	/**
	 * 获取类型下拉选择数据
	 * 
	 * @param id
	 *            选择的大类
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<ClassifyVO> getSelectType(String id) {
		List<Classify> list = (List<Classify>) dao.queryByHql("from " + Classify.class.getName());
		return doSelectType(list, id, 0);
	}

	/**
	 * 获取大类下的二级菜单
	 * 
	 * @param id
	 *            选择的大类
	 * @return
	 */
	public List<ClassifyVO> getLevel2(String id) {
		return dao.getLevel2(id);
	}

	/**
	 * 获取一个节点下所有子节点形成的id->选择的节点
	 * 
	 * @param sou
	 * @param parentId
	 * @return
	 */
	private static List<ClassifyVO> doSelectType(List<Classify> sou, String id, int level) {
		List<ClassifyVO> result = new ArrayList<ClassifyVO>();
		for (Classify g : sou) {
			if (id.equals(g.getPid())) {
				ClassifyVO vo = new ClassifyVO();
				vo.setClassId(g.getClassId());
				vo.setClassName(getLine(level, g.getClassName()));
				vo.setPid(g.getPid());
				result.add(vo);
				List<ClassifyVO> son = doSelectType(sou, g.getClassId(), level + 1);
				if (son.size() > 0) {
					result.addAll(son);
				}
			}
		}
		return result;
	}

	private static String getLine(int n, String name) {
		if (0 == n) {
			return "——&nbsp;" + name + "&nbsp;——";
		} else {
			return name;
		}
	}

	private Map<ClassifyVO, Map<ClassifyVO, List<ClassifyVO>>> frontMap = null;

	/**
	 * 前端获取分类树
	 * 
	 * @param id
	 * @return
	 */
	public Map<ClassifyVO, Map<ClassifyVO, List<ClassifyVO>>> getFrontClass() {
		if (null == frontMap) {
			getFrontClassByDb();
		}
		return frontMap;
	}

	/**
	 * 前端Map树查询数据库获取数据方法
	 */
	@SuppressWarnings("unchecked")
	public void getFrontClassByDb() {
		Map<ClassifyVO, Map<ClassifyVO, List<ClassifyVO>>> map = new LinkedHashMap<ClassifyVO, Map<ClassifyVO, List<ClassifyVO>>>();
		List<ClassifyVO> list = (List<ClassifyVO>) dao.queryByHql("select new " + ClassifyVO.class.getName() + ClassifyVO.STRUCT + " from " + Classify.class.getName() + " classify order by classify.orderKey");
		List<ClassifyVO> top = getDirectSon(list, "");
		for (ClassifyVO c : top) {
			Map<ClassifyVO, List<ClassifyVO>> m2 = new LinkedHashMap<ClassifyVO, List<ClassifyVO>>();
			List<ClassifyVO> a2 = getDirectSon(list, c.getClassId());
			for (ClassifyVO c2 : a2) {
				List<ClassifyVO> a3 = getDirectSon(list, c2.getClassId());
				m2.put(c2, a3);
			}
			map.put(c, m2);
		}
		frontMap = map;
	}

	/**
	 * 获取子节点数据
	 * 
	 * @param list
	 * @param id
	 * @return
	 */
	private List<ClassifyVO> getDirectSon(List<ClassifyVO> list, String id) {
		List<ClassifyVO> tmp = new ArrayList<ClassifyVO>();
		for (ClassifyVO c : list) {
			if (id.equals(c.getPid())) {
				tmp.add(c);
			}
		}
		return tmp;
	}
}
